<script setup>
import { ref } from 'vue';
import axios from 'axios';
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();
const form = ref(null);
const username = ref('');
const password = ref('');

const handleLogin = async () => {
  try {
    const response = await axios.post('http://localhost:8085/user/login', {
      username: username.value,
      password: password.value,
    },{
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    });
    if (response.data === '登录成功') {
      alert('登录成功');
      router.push('/index');
    } else {
      alert('登录失败，请检查您的用户名和密码');
    }
  } catch (error) {
    console.error('请求失败:', error);
    alert('登录请求发送失败');
  }
};

</script>

<template>
  <div class="container">
    <div class="login-wrap">
      <el-form class="login-container" ref="form">
        <h1 class="title">用户登录</h1>
        <el-form-item prop="username">
          <el-input v-model="username" type="text" placeholder="用户账号"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="password" type="password" placeholder="用户密码"
                    show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<style scoped>
.container{
  width: 100%;
  height: 100vh;
  display: flex;
  color: white;
  background-image: url('/大屏背景.png');
  background-size: cover; /* 使背景图像覆盖整个元素 */
  background-position: center; /* 使背景图像居中显示 */
}
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.register-container{
  display: flex;
  align-items: center;
  justify-content: space-around;
}
</style>